﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using KC.Framework.Tenant;
using KC.Service.Account;
using KC.Service.Core;
using KC.Service.Core.Constants;
using KC.DTO.Account;
using KC.DTO.Core;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;

namespace KC.Web.Account.Controllers
{
    public abstract class AccountBaseController : MVC.Core.Controllers.TenantWebBaseController
    {
        protected ISysManageService SysManageService => ServiceProvider.GetService<ISysManageService>();
        protected IAccountService AccountService => ServiceProvider.GetService<IAccountService>();

        public AccountBaseController(
            Tenant tenant,
            IServiceProvider serviceProvider,
            ILogger logger)
            : base(tenant, serviceProvider, logger)
        {
        }

        /// <summary>
        /// 树形选中
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="checkList">选中的节点</param>
        /// <param name="scoreList">所有节点</param>
        /// <returns></returns>
        protected List<T> GetTreeNodeSimpleWithChildren<T>(ICollection<int> checkList, List<T> scoreList) where T : TreeNodeSimpleDTO<T>
        {
            foreach (var dto in scoreList)
            {
                if (dto.children.Any())
                {
                    if (checkList.Contains(dto.id))
                        dto.@checked = true;
                    GetTreeNodeSimpleWithChildren(checkList, dto.children);
                }
                else
                {
                    if (checkList.Contains(dto.id))
                    {
                        dto.@checked = true;
                    }
                }
            }
            return scoreList;
        }

        #region Private

        /// <summary>
        /// 当前菜单角色
        /// </summary>
        /// <param name="menulist"></param>
        /// <param name="rolelist"></param>
        /// <returns></returns>
        protected List<RoleSimpleDTO> GetCheckRoleList(List<string> checkList, List<RoleSimpleDTO> rolelist)
        {
            foreach (var dto in rolelist)
            {
                if (checkList.Contains(dto.RoleId))
                {
                    dto.@checked = true;
                }
            }

            return rolelist;
        }

        /// <summary>
        /// 清除某角色下所有用户的缓存
        /// </summary>
        /// <param name="roleIds">角色Id集合</param>
        protected void ClearUserCacheByRoles(List<string> roleIds)
        {
            var users = AccountService.GetUsersByRoleIds(roleIds);
            foreach (var user in users)
            {
                //清除用户缓存
                RemoveCachedCurrentUser(CurrentUserTenantName, user.UserId);
            }
        }

        /// <summary>
        /// 清除某用户的缓存
        /// </summary>
        /// <param name="userId"></param>
        protected void ClearUserCache(string userId)
        {
            var cacheKey = TenantName.ToLower() + "-" + CacheKeyConstants.Prefix.CurrentUserId + userId;
            CacheUtil.RemoveCache(cacheKey);
        }
        #endregion
    }
}